<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>修改密码</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../../css/public.css" media="all"/>
</head>
<body class="childrenBody">
<form class="layui-form layui-row changePwd" lay-filter="dataPwd" id="dataPwd">
    <div class="layui-col-xs12 layui-col-sm6 layui-col-md6">
        <div class="layui-form-item">
            <label class="layui-form-label" >用户名</label>
            <div class="layui-input-block">
                <input type="text" id="username" value="${user.loginname}" disabled class="layui-input layui-disabled">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">旧密码</label>
            <div class="layui-input-block">
                <input type="password" value="" name="oldPwd" placeholder="请输入旧密码" lay-verify="required|oldPwd"
                       class="layui-input pwd">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-block">
                <input type="password" value="" name="newPwd" placeholder="请输入新密码" lay-verify="required|newPwd" id="oldPwd"
                       class="layui-input pwd">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-block">
                <input type="password" value="" name="confirmPwd" placeholder="请确认密码" lay-verify="required|confirmPwd"
                       class="layui-input pwd">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="changePwd">立即修改</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </div>
</form>
<script type="text/javascript" src="${yeqifu}/static/layui/layui.js"></script>
<script src="${yeqifu}/static/layui/layui.all.js"></script>
<script type="text/javascript">

    layui.use(['jquery', 'layer', 'form'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        form.on("submit(changePwd)", function(data){
            var loginname = document.getElementById("username").value;
            var datas = {loginname: loginname,pwd: data.field.newPwd};
            // 这里请求后台
            $.ajax({
                url:"/user/changePwd",
                data:JSON.stringify(datas),
                dataType:'JSON',
                contentType : "application/json",
                type:'POST',
                success :function (result) {
                    layer.msg(result.msg);
                    if(result.code == 0){
                        console.log(1111)
                        setTimeout(function () {
                            top.location.href="${yeqifu}/login/quit"
                        },1000)
                    }
                }
            })

            return false;
        });


        //添加验证规则
        form.verify({
            oldPwd: function (value) {
                var loginname = document.getElementById("username").value;
                var datas = {loginname: loginname,pwd: value};
                var message = '';
                $.ajax({
                    type: "POST",
                    url: "/user/verifyUserOldPwd",  //指的是 对应后台的方法
                    async: false, //改为同步请求
                    contentType: 'application/json;charset=UTF-8',
                    data: JSON.stringify(datas),
                    dataType: 'json',
                    success: function (data) {
                        if (data) {

                        } else {
                            message = "原密码错误，请重新输入！"
                        }
                    }
                });
                //需要注意 需要将返回信息写在ajax方法外
                if (message !== '') {
                    return message
                }

            },
            newPwd: function (value, item) {
                if (value.length < 6) {
                    return "密码长度不能小于6位";
                }
            },
            confirmPwd: function (value, item) {
                if (!new RegExp($("#oldPwd").val()).test(value)) {
                    return "两次输入密码不一致，请重新输入！";
                }
            }
        })
    });
</script>
</body>
</html>
